package com.code;

import com.code.config.DataSourceInfo;
import com.code.generator.provider.MetadataProvider;
import com.code.generator.code.impl.CodeGenerateManager;
import com.code.entity.CodeGenerateOption;
import com.code.enums.CodeGenerateProviderType;
import com.code.enums.CodeGenerateType;
import com.code.entity.GenerateDescriptor;
import com.code.entity.metadata.Table;
import com.code.generator.sqlgenerator.GenerateSql;
import com.code.generator.sqlgenerator.initexcel.ExcelGenerator;
import com.code.utils.DataProcessUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@SpringBootApplication
public class CodeEasyApplication {

    public static void main(String[] args) {
        List<String> tableNames = Arrays.asList("user_info");
        String basePackage = "com.project";
        String modelName = "project";
        generateCode(tableNames, basePackage, modelName);
        // generateSql(args);
        // DictGenerator.generate();
        // test();
        // JobGenerator.generate();
    }


    /**
     * 生成Sql
     *
     * @param args
     */
    private static void generateSql(String[] args) {
        GenerateSql.generate(args);
    }

    /**
     * 生成初始excel
     */
    private static void generateExcel() {
        ExcelGenerator.generateExcel();
    }


    /**
     * @param tableNames  表名列表
     * @param basePackage 项目基础包
     * @param modelName   项目模块名称
     */
    private static void generateCode(List<String> tableNames, String basePackage, String modelName) {
        CodeGenerateOption.setDefaultBasePackage(basePackage);
        String currentDir = System.getProperty("user.dir") + "/src/main/";
        CodeGenerateOption.setDefaultOutputDir(currentDir);
        List<CodeGenerateOption> generateOptions = new ArrayList<CodeGenerateOption>() {
            {
                for (String tableName : tableNames) {
                    add(new CodeGenerateOption(tableName, modelName, basePackage));
                }
            }
        };

        log.info(" 开始生成代码...输出代码地址：" + CodeGenerateOption.getDefaultOutputDir());
        MetadataProvider provider = DataSourceInfo.getMetadataProvider();

        // 获取表格列表
        List<Table> allTables = provider.getTables();
        if (allTables == null || allTables.isEmpty()) {
            return;
        }
        Map<String, Table> tableMappings = allTables.stream()
                .collect(Collectors.toMap(Table::getName, table -> table));
        DataProcessUtils.validTableField(tableMappings);
        List<GenerateDescriptor> descriptors = generateOptions.stream()
                .map(option -> GenerateDescriptor.builder().basePackage(option.getBasePackage())
                        .module(option.getModuleName()).useProvider(true)
                        .table(tableMappings.get(option.getTableName())).generateTypes(CodeGenerateType.values())
                        .destination(option.getOutputDir()).commonPackage(option.getCommonPackage())
                        .build()).collect(Collectors.toList());
        CodeGenerateManager.generate(CodeGenerateProviderType.VELOCITY, descriptors);
        log.info("代码生成成功，总共 " + generateOptions.size()
                + " 张表......代码输出地址：" + CodeGenerateOption.getDefaultOutputDir());
    }


}
